//
//  UILabel+Ext.swift
//  TTCOM-iOS
//
//  Created by Toj on 2/10/23.
//

import UIKit

public extension UILabel {
    
    convenience init(font size: CGFloat, textColor hex: String) {
        self.init()
        textColor = hex.to_color
        font = size.to_boldFont
    }
    
    convenience init(font size: CGFloat, textColor hex: Int) {
        self.init()
        textColor = hex.to_color
        font = size.to_boldFont
    }
}

public extension UILabel {
    
    var st_isEmpty: Bool {
        return lbl_isEmpty
    }
    
    var lbl_isEmpty: Bool {
        return text == nil || text?.count == 0
    }
    
    func cm_stringSize(maxSize: CGSize) -> CGSize {
        guard let string = text else { return .zero }
        return string.cm_stringSize(font: font, maxSize: maxSize)
    }
    
    func cm_stringHeight(maxWidth: CGFloat) -> CGFloat {
        let maxSize = CGSize(width: maxWidth, height: CGFloat(MAXFLOAT))
        return cm_stringSize(maxSize: maxSize).height
    }
    
    var cm_stringOneLineHeight: CGFloat {
        return cm_stringHeight(maxWidth: CGFloat(MAXFLOAT))
    }
    
    /** 计算宽度 +1 为了容错 */
    func cm_stringWidth(maxHeight: CGFloat) -> CGFloat {
        let maxSize = CGSize(width: CGFloat(MAXFLOAT), height: maxHeight)
        return cm_stringSize(maxSize: maxSize).width + 1
    }
    
    var cm_stringOneLineWidth: CGFloat {
        return cm_stringWidth(maxHeight: CGFloat(MAXFLOAT))
    }
    
    // 同上
    var cm_stringOneLineWidth1: CGFloat {
        return cm_stringWidth(maxHeight: 1)
    }
}
